///////////////////////////////////////////////////////////////////////////////////////
//
//	AvaMods Daily Reboot
//		- Version 1.0
//		- 05.28.2007
//		- diamond-optic
//
//////////////////////////////////////////////////////////////////////////////////
//
// Credit:
//
//	- Watch (vaultdata stuff)
//
//////////////////////////////////////////////////////////////////////////////////
//
// Information:
//
//	- Will reboot your server once a day
//	- Checks to make sure there are no real players
//	- If there is only one player, checks to see if they are spec (afk)
//	- Will only reboot if its between 3AM & 6AM
//
//	* WARNING - this will wipe out any temporary bans
//
//////////////////////////////////////////////////////////////////////////////////
//
// Change Log:
//
//	- 05.28.07 - Version 1.0
//		Initial Release
//
///////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////
// Module Include
//
#include <amxmodx>

///////////////////////////////////////////////////////////////////////////////////////
// Plugin Init
//
public plugin_init()
{
	register_plugin("AvaMods Daily Reboot","1.0","diamond-optic")
	
	register_cvar("avamods_dailyreboot_stats", "1.0", FCVAR_SERVER|FCVAR_SPONLY)
	
	set_task(180.0,"funcCheck")
}

///////////////////////////////////////////////////////////////////////////////////////
//
// Time/Player check function
//
public funcCheck()
{
	new time[3]
	get_time("%H",time,2)
	
	if(equali(time,"03") || equali(time,"3") || equali(time,"04") || equali(time,"4") || equali(time,"05") || equali(time,"5"))
		{
		new today[12], server_reboot[12]
		get_time("%m/%d/%Y",today,11)
	
		if(!vaultdata_exists("server_reboot")) 
			set_vaultdata("server_reboot",today)
	
		get_vaultdata("server_reboot",server_reboot,11)
		
		if(!equal(today,server_reboot)) 
			{
			new players[32], num
			get_players(players,num,"c")
			
			if(num == 0)
				funcReboot(today,server_reboot,num)
			else if(num == 1)
				{
				new lonely_player = players[0]
					
				new team = get_user_team(lonely_player)
				
				if(team != 1 && team != 2)
					{
					server_cmd("kick #%d ^"Daily Server Reboot^"", get_user_userid(lonely_player))	
						
					funcReboot(today,server_reboot,num)
					}
				}				
			}
		}
}

///////////////////////////////////////////////////////////////////////////////////////
//
// Reboot Function
//
public funcReboot(today[],server_reboot[],players)
{
	set_vaultdata("lastrestarted",today)
	
	log_amx("Restarting: (Last Restart: %s) (Today: %s) (Clients: %d)",server_reboot, today, players)
	
	server_cmd("quit") 	
}
